$(function() {
    var shop = cookie.get('shop');
    $('.cart-list-table tbody').empty();
    if (shop) {
        var arr = []
        shop = JSON.parse(shop);
        for (let i in shop) {
            if (shop[i].type == 1) {
                $.ajax({
                    type: "get",
                    url: "../lib/discount_detail.php",
                    data: {
                        'id': shop[i].id
                    },
                    dataType: "json",
                    success: function(res) {
                        var pic = JSON.parse(res.pic);
                        var template = `
                        <tr>
                            <th><input type="checkbox" class="check" data-sum=${(res.price)*shop[i].num}></th>
                            <td style="text-align:left">
                                <img src=${pic[2].src}>
                                <a href="javascript:;">${res.title}</a>
                            </td>
                            <td>皮肤</td>
                            <td>${res.cost} Q币</td>
                            <td>永久</td>
                            <td>${shop[i].num}</td>
                            <td>限时折扣</td>
                            <td>${(res.price)*shop[i].num} Q币</td>
                            <td><a href="javascript:;" class="btn-clear" data-id=${res.id} data-type=1>删除</a></td>
                        </tr>
                        `;
                        $('.cart-list-table tbody').append(function(index, value) {
                            return template;
                        });
                    }
                });

            } else if (shop[i].type == 2) {
                $.ajax({
                    type: "get",
                    url: "../lib/product_detail.php",
                    data: {
                        'id': shop[i].id
                    },
                    dataType: "json",
                    success: function(res) {
                        var pic = JSON.parse(res.pic);
                        var template = `
                        <tr>
                            <th><input type="checkbox" class="check" data-sum=${(res.price)*shop[i].num} ></th>
                            <td style="text-align:left">
                                <img src=${pic[2].src}>
                                <a href="javascript:;">${res.title}</a>
                            </td>
                            <td>${res.type}</td>
                            <td>${res.price} Q币</td>
                            <td>${res.time}</td>
                            <td>${shop[i].num}</td>
                            <td>无优惠</td>
                            <td>${(res.price)*shop[i].num} Q币</td>
                            <td><a href="javascript:;" class="btn-clear" data-id=${res.id} data-type=2>删除</a></td>
                        </tr>
                        `;
                        $('.cart-list-table tbody').append(function(index, value) {
                            return template;
                        });
                    }
                });
            }

        }
    }
    // 删除按钮
    $('.cart-list-table tbody').on('click', '.btn-clear', function() {
        if (confirm('确认要删除吗？')) {
            var id = this.dataset.id;
            var type = this.dataset.type;
            var shop = cookie.get('shop');
            shop = JSON.parse(shop);

            for (var i in shop) {
                if (shop[i].id == id && shop[i].type == type) {
                    shop.splice(i, 1);
                    cookie.set('shop', JSON.stringify(shop), 1);
                    $(this.parentNode.parentNode).remove();
                }
            }
        }
    });
    var sum = 0,
        cart_sum = 0;
    // 复选框选择功能
    $('.cart-list-table tbody').on('click', '.check', function() {
        if (cart_sum > 10) {
            alert('超过最大购买数量');
        } else {
            // 点击后被选中
            var sum_ = parseFloat(this.dataset.sum);
            if (this.checked) {
                sum += sum_;
                cart_sum++;
            } else { //点击后取消
                sum -= sum_;
                cart_sum--;
            }
            $('.cart_add_num').html(sum.toFixed(2));
            $('.sum-num').html(sum.toFixed(2));
            // 判断列表个数
            $('#cart_current_num').html(cart_sum);
            $('#cart_status').html('(' + cart_sum + '/10)');
            // 商品总计的出现
            if (cart_sum > 0) {
                $('.cart-total-box').css('display', 'block');
                $('.btn-submit a').css('background', '#f74a4a').addClass('true');
            } else {
                $('.cart-total-box').css('display', 'none');
                $('.btn-submit a').css('background', '#666').removeClass('true');
            }
        }
    });
    // 提交订单付款功能
    $('.btn-submit a').on('click', function() {
        if ($(this).hasClass('true')) {
            $('.midas_minipay_dialog_mask').css('display', 'block');
            $('.midas_minipay_dialog_wrap_no_border').css('display', 'block');
            $('.prop-price em').html($('.sum-num').html());
            $('.account-id').html(cookie.get('username'));
        }
    });
    // 关闭订单功能
    $('.header-icon-last').on('click', function() {
        $('.midas_minipay_dialog_mask').css('display', 'none');
        $('.midas_minipay_dialog_wrap_no_border').css('display', 'none');
    });
});